import Vue from 'vue';
import Vuex from 'vuex';
import VuexPersistence from "vuex-persist";

Vue.use(Vuex)
import axios from "axios";

export default new Vuex.Store({
  state: {
    data1:[],
    data2:[],
    data3:[],
    cartList:[],
    ck:[]
  },
  mutations: {
    gataData(state,item){
      state.data1 = [...item];
    },
    gataData1(state,item){
      state.data2 = [...item];
    },
    gataData2(state,item){
      state.data3 = [...item];
    },
    ADDCART(state,item){
      var rel = false;
      state.cartList.forEach(el=>{
        console.log(el.item.productId)
        console.log(item.productId)
        if(el.item.productId == item.productId){
          el.num++;
          rel = true;
        }
      })
      if(!rel){
        state.cartList.push({item,num:1,checked:false})
      }
    },
    CHANGECK(state,index){
      if(state.cartList[index].checked ===true){
        state.cartList[index].checked = false
      }else {
        state.cartList[index].checked = true
      }
    },
    DELETE(state,index){
      state.cartList.splice(index,1)
    },
    CHECKALL(state,e){
      state.cartList.forEach(el=>{
        if(e===true){
          el.checked = true
        }else if(e ===false){
          el.checked = false
        }
      })
    },
    DELCHECK(state){
      state.cartList.forEach((el,index)=>{
        if(el.checked === true){
          console.log(index)
          state.cartList.splice(index,1)
        }
      })
    }
  },
  actions: {
    GATADATA(conext){
      axios.get("data1.json").then(res=>{
        conext.commit("gataData",res.data.result.list)
      });
      axios.get("data2.json").then(res=>{
        conext.commit("gataData1",res.data.result.list)
      });
      axios.get("data3.json").then(res=>{
        conext.commit("gataData2",res.data.result.list)
      })
    }
  },
  modules: {
  },
  plugins: [
    new VuexPersistence({
      storage: window.localStorage,
    }).plugin,
  ],
})
